﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace SSP.Api
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class DirectorWrapper
    {
        private readonly Director parent;

        /// <summary>
        /// Initializes a new instance of the <see cref="DirectorWrapper"/> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        public DirectorWrapper(Director parent)
        {
            this.parent = parent;
        }

        /// <summary>
        /// Gets the parent.
        /// </summary>
        /// <value>The parent.</value>
        public Director Parent
        {
            get { return parent; }
        }

        /// <summary>
        /// Bools to int.
        /// </summary>
        /// <param name="val">if set to <c>true</c> [val].</param>
        /// <returns></returns>
        protected static string BoolToInt(bool val)
        {
            return (val ? 1 : 0).ToString();
        }

        /// <summary>
        /// Converts to int.
        /// </summary>
        /// <param name="str">The STR.</param>
        /// <returns></returns>
        protected static int convertToInt(string str)
        {
            double ret;
            return Convert.ToInt32(double.TryParse(str, out ret) ? ret : 0);
        }

        /// <summary>
        /// Gets the users.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        protected DirectorUser[] getUsers(XmlReader reader)
        {
            var list = new List<DirectorUser>();

            // Start to use the reader.
            reader.Read();

            // Read all elements
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                switch (reader.Name)
                {
                    case "user":
                        var cont = new DirectorUser(Parent);
                        cont.DeserializeFromReader(reader);
                        list.Add(cont);
                        break;
                    default:
                        throw new Exception("Unhandled xml element name '" + reader.Name + "'.");
                }
                reader.MoveToContent();
            }

            // Extremely important to read the node to its end.
            // next call of the reader methods will crash if not called.
            reader.ReadEndElement();

            return list.ToArray();
        }

        /// <summary>
        /// Gets the galeries.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        protected DirectorGallery[] getGaleries(XmlReader reader)
        {
            var list = new List<DirectorGallery>();

            // Start to use the reader.
            reader.Read();

            // Read all elements
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                switch (reader.Name)
                {
                    case "gallery":
                        var cont = new DirectorGallery(Parent);
                        cont.DeserializeFromReader(reader);
                        list.Add(cont);
                        break;
                    default:
                        throw new Exception("Unhandled xml element name '" + reader.Name + "'.");
                }
                reader.MoveToContent();
            }

            // Extremely important to read the node to its end.
            // next call of the reader methods will crash if not called.
            reader.ReadEndElement();

            return list.ToArray();
        }

        /// <summary>
        /// Gets the albums.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        protected DirectorAlbum[] getAlbums(XmlReader reader)
        {
            var list = new List<DirectorAlbum>();

            // Start to use the reader.
            reader.Read();

            // Read all elements
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                switch (reader.Name)
                {
                    case "album":
                        var cont = new DirectorAlbum(Parent);
                        cont.DeserializeFromReader(reader);
                        list.Add(cont);
                        break;
                    default:
                        throw new Exception("Unhandled xml element name '" + reader.Name + "'.");
                }
                reader.MoveToContent();
            }

            // Extremely important to read the node to its end.
            // next call of the reader methods will crash if not called.
            reader.ReadEndElement();

            return list.ToArray();
        }


        /// <summary>
        /// Gets the externals.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        protected External[] getExternals(XmlReader reader)
        {
            var list = new List<External>();

            // Start to use the reader.
            reader.Read();

            // Read all elements
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                switch (reader.Name)
                {
                    case "external":
                        var cont = new External();
                        cont.DeserializeFromReader(reader);
                        list.Add(cont);
                        break;
                    default:
                        throw new Exception("Unhandled xml element name '" + reader.Name + "'.");
                }
                reader.MoveToContent();
            }

            // Extremely important to read the node to its end.
            // next call of the reader methods will crash if not called.
            reader.ReadEndElement();

            return list.ToArray();
        }

        /// <summary>
        /// Gets the contents.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        protected DirectorContent[] getContents(XmlReader reader)
        {
            var list = new List<DirectorContent>();

            // Start to use the reader.
            reader.Read();

            // Read all elements
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                switch (reader.Name)
                {
                    case "content":
                        var cont = new DirectorContent(Parent);
                        cont.DeserializeFromReader(reader);
                        list.Add(cont);
                        break;
                    default:
                        throw new Exception("Unhandled xml element name '" + reader.Name + "'.");
                }
                reader.MoveToContent();
            }

            // Extremely important to read the node to its end.
            // next call of the reader methods will crash if not called.
            reader.ReadEndElement();

            return list.ToArray();
        }
    }
}